﻿using System;
using System.Diagnostics;
using Microsoft.Samples.WindowsAzure.ServiceManagement;

namespace InstanceControllerWorker.ManageCommands
{
	public partial class CSManageCommand
	{
		public static string OperationId { get; set; }
	}

	/// <summary>
	/// Implements GetResult Command for getting status of Asynchronous operation
	/// </summary>
	public class GetResultCommand : CSManageCommand
	{

		/// <summary>
		/// Performs the operation. Overrides the virtual command. Manually calling this command
		/// causes the Service status to be updated in the state management store.
		/// </summary>
		/// <param name="channel">The channel.</param>
		protected override void PerformOperation(IServiceManagement channel)
		{
			Trace.WriteLine("Getting Operation Status", "Information");
			// Checking to see if the operation at the specified ID has completed by retrieving the status.
			// http://msdn.microsoft.com/en-us/library/ee460783.aspx
			var operation = channel.GetOperationStatus(SubscriptionId, OperationId);
			Trace.WriteLine(String.Format("Requested Status={0}", operation.Status), "Information");
			if (operation.Error != null)
			{
				Trace.WriteLine(operation.Error, "Information");
			}

			//Todo: Update Service status in state management store
		}
	}

	/// <summary>
	/// Implements GetStatus Command for getting status of running services
	/// </summary>
	public class GetStatusCommand : CSManageCommand
	{
		protected override void PerformOperation(IServiceManagement channel)
		{
			Deployment deployment = null;
			Trace.WriteLine("Getting Status", "Information");

			deployment = channel.GetDeploymentBySlot(SubscriptionId, HostedServiceName, DeploymentSlot);
			//Todo: update the Instances table.
			//var instancesDataModel = new InstancesDataModel(partitionKey, rowKey);
			//instancesDataModel.InstanceName = deployment.Status;

		}

	}
}
